package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 套餐分页查询
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<Setmeal> page =setmealMapper.page(pageQueryDTO);
        List<SetmealVO> list = new ArrayList<>();
        page.forEach(setmeal->{
            SetmealVO vo = new SetmealVO();
            BeanUtils.copyProperties(setmeal,vo);
            //获取套餐的名称
            String categoryName = categoryMapper.getNameByID(setmeal.getCategoryId().intValue());
            // 查询套餐和菜品的关系
            List<SetmealDish> dishes = setmealDishMapper.getDishBySetmealID(setmeal.getId());
            vo.setSetmealDishes(dishes);
            vo.setCategoryName(categoryName);
            list.add(vo);
        });

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(list);
        return pageResult;
    }

    /**
     * 套餐状态修改
     * @param status
     */
    @Override
    public void status(Integer status,Integer id) {
        setmealMapper.status(status,id);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        //添加套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal,"setmealDishes");
        setmealMapper.insert(setmeal);
        //将套餐中包含的菜品信息查询出来
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(dish->{
            dish.setSetmealId(setmeal.getId());
        });
        //调用mapper将信息存入对应的表中
        setmealDishMapper.saveSetmealDish(setmealDishes);
    }

    /**
     * 根据id寻找套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealByID(Long id) {
       SetmealVO setmealVO = setmealMapper.getSetmealByID(id);
        //根据套餐id寻找对应的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetmealID(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //更新套餐包含的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //删除原来套餐包含的菜品
        setmealDishMapper.deleteDish(setmealDTO.getId());
        //插入新的菜品
        setmealDishMapper.saveSetmealDish(setmealDishes);
        setmealMapper.update(setmealDTO);
    }

    /**
     * 删除套餐
     * @param lists
     */
    @Override
    @Transactional
    public void delete(String[] lists) {
        for (int i = 0; i < lists.length; i++) {
            long id = Long.parseLong(lists[i]);
            //1.查看套餐的状态
            Setmeal setmeal = setmealDishMapper.getSetmealByID(id);
            if(setmeal.getStatus()==StatusConstant.ENABLE){
                //套餐正在售卖  不能删除
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealDishMapper.deleteDish(id);
            setmealMapper.delete(id);
        }

    }
}
